﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace LoginServer
{
    public class ManagerRoom
    {
        Hashtable rooms;
        public ManagerRoom(GetDB AccessDB)
        {
            rooms = new Hashtable();
            Init(AccessDB);
        }
        public void Init(GetDB AccessDB)
        {
            List<RoomInfo> listRoom = AccessDB.GetAllRoom();
            if(listRoom!=null)
                foreach (var roominfo in listRoom)
                {
                    AddRoom(new Room(roominfo));
                    Room room = GetRoom(roominfo.RoomID);
                    room.LoadTable(AccessDB);
                    room.LoadPlayer(AccessDB);
                    
                }
        }
        public Hashtable Rooms(){
            return rooms;
        }
        public Room GetRoom(int key)
        {            
            return (Room)rooms[key];
        }
        public void AddRoom(Room room)
        {
            rooms.Add(room.info.RoomID, room);
        }
        public void RemoveRoom(Room room)
        {
            rooms.Remove(room.info.RoomID);
        }

        public int GetNewID()
        {
            if (rooms.Count < Program.MaxRoom)
                return rooms.Count;
            return -1;
        }
        public bool CheckRoomID(int ID)
        {
            if (rooms.ContainsKey(ID))
                return true;
            Console.WriteLine("RoomID not exits");
            return false;
        }
        public bool CheckNotFullPlayerInRoom(int ID)
        {
            Room room = (Room)rooms[ID];
            if (room.Manager_Player.Players.Count < Program.MaxRoom)
                return true;
            Console.WriteLine("FullPlayer");
            return false;
        }

        internal bool CheckRoomOutID(int ID)
        {
            if (!rooms.ContainsKey(ID))
                return true;
            Console.WriteLine("RoomID exits");
            return false;
        }
    }
}
